;(function(go){
    let ajex = function(method , url , data){//（method ， url [,dataOPtions]）

        return new Promise((resolve , reject) => {
            let request;
            let str = '';
            (function(method , url , data){//ajax入口（method ， url [,dataOPtions]）
                if (window.XMLHttpRequest) {// 新建XMLHttpRequest对象,兼容ie
                    request = new XMLHttpRequest();
                } else {
                    request = new ActiveXObject('Microsoft.XMLHTTP');
                }
                request.onreadystatechange = function () { // 状态发生变化时，函数被回调
                    if (request.readyState === 4) { // 成功完成
                        // 判断响应结果:
                        if (request.status === 200) {
                            // 成功，通过responseText拿到响应的文本:
                            resolve(request.responseText);
                        } else {
                            // 失败，根据响应码判断失败原因:
                            reject(request.status);
                        }
                    } else {
                        // HTTP请求还在继续...
                    }
                }
                
                // 发送请求:
                method = method.toUpperCase();//将方式转换为大写容错
                if(method === 'GET' && data){
                    url += '?';
                    for(let i in data){
                        url = url + i + '=' + data[i] + '&';
                    }
                    request.send();
                }
                request.open(method, url);
                if(method === 'POST'){
                    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    if(data){
                        for(let i in data){
                            str = str + i + '=' + data[i] + '&';
                        }
                    }
                    request.send(str);
                }
            })(method , url , data);
        }) 
    };
    let setClass = function(dom, target, newtar){//设置class
        if (target === '') {
            dom.className = dom.className + ' ' + target;
        } else {
            let reg = new RegExp('\\b' + target + '\\b');
            dom.className = dom.className.replace(reg, newtar);
        }
    };
    let getTop = function(e) {
        var offset = e.offsetTop;
        if (e.offsetParent != null) offset += getTop(e.offsetParent);
        return offset;
    };
    let isShown = function(dom , cb){//dom 是否在可视区域，是则去掉coming类
        let hideHeight = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
        let windowHeight = document.documentElement.clientHeight < document.body.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
        let domY = getTop(dom);
        if (domY > hideHeight && domY < hideHeight + windowHeight) {
            cb();
            return true;
        }
    };
    let hidden = function( f){
        let h = {};
        h.y = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
        h.x = document.documentElement.scrollLeft || window.pageXOffset || document.body.scrollLeft;
        return h[f];
    };
    let windowSize = function(f){
        let size = {};
        size.y = document.documentElement.clientHeight < document.body.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
        size.x = document.documentElement.clientWidth < document.body.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
        return size[f];
    };
    let icookie = {
        put : function(name , value , options){
            let str = name + '=' + value;
            if(options['max-age']){
                atr += ';max-age=' + options['max-age'];
            };
            if(options.expires){
                atr += ';expires=' + options.expires;
            };
            if(options.path){
                atr += ';path=' + options.path;
            };
            if(options.domain){
                atr += ';domain=' + options.domain;
            };
            if(options.secure){
                atr += ';secure=' + options.secure;
            }
            document.cookie = str;
        },
        take : function(name){//可用正则优化
            let arr = document.cookie.split(';');
            let len = arr.length;
            let item = [];
            for(let i = 0 ; i < len ; i++){
                item = arr[i].split('=')
                if(item[0] === name){
                    return item[1];
                }
            }
        },
        delete : function(name){
            this.put(name , '' , {'max-age':0});
        }
    };
    function init (){
        go.ijax = ajax;
        go.setClass = setClass;
        go.getTop = getTop;
        go.isShown = isShown;
        go.hidden = hidden;
        go.windowSize = windowSize;
        go.icookie = icookie;
    }
})(window.itool || (window.itool = {}))